<?php

namespace app\common\model;

use app\user\logic\user\VipUser;
use think\facade\Request;
use think\Model;

abstract class BaseModel extends Model
{
    protected static $listRows = null;
    protected static $mid = 0;

    // 模型初始化
    protected static function init(): void
    {
        static::$mid      = request()->mid;
        static::$listRows = Request::param('count/d', 20);
    }

    /**
     * 根据用户身份返回价格
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-01
     *
     * @param  [type] $data [description]
     */
    public static function getUserPrice($data): float
    {
        // 定义身份价格的初始价为销售价
        $user_price = $data['price'] ?? 0;

        if ($user_price && $user_price != 0) {
            // 会员(涉及到免费和会员免费区别)
            if (isset($data['vip_price']) && $data['vip_price']) {
                $is_vip     = app(VipUser::class)->isVip();
                $user_price = $is_vip ? min($data['vip_price'], $data['price']) : $data['price'];
            }

            // 活动价
            if (isset($data['positive_promotion']) && count($data['positive_promotion'])) {
                $user_price = min($user_price, $data['positive_promotion']['price']);
            }
        }

        return round($user_price, 2);
    }

    /**
     * 根据用户返回所有价格数组
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-01
     *
     * @param  [type] $data [description]
     */
    public function getMzPrice($data): array
    {
        if ($data['price'] == 0) {
            $final_price = 0;
        } else {
            // 会员(涉及到免费和会员免费区别)、活动、
            $is_vip      = app(VipUser::class)->isVip();
            $final_price = $is_vip ? min($data['vip_price'], $data['price']) : $data['price'];
            // 是否有活动模块
            // $event_price = ;
            // $final_price = $data->vip_price > $event_price ? $event_price : $vip_price;
        }

        return [
            'price'            => $data['price'],
            'scribing_price'   => $data['scribing_price'],
            'vip_price'        => $data['vip_price'],
            'final_price'      => $final_price,
            'event_price'      => 0,
            'asb_create_price' => 0,
            'asb_join_price'   => 0,
        ];
    }

    public static function getListRows()
    {
        return static::$listRows;
    }

    /**
     * 取得当前验证器实例
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-02-05
     */
    final public function getValidate($class = null)
    {
        if (is_null($class)) {
            $class = static::class;
            $class = str_replace('\\logic\\', '\\validate\\', $class);
        }

        return new $class;
    }

    /**
     * 分类数据
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-07
     *
     * @param    [type]                         $category [description]
     * @param    [type]                         $data     [description]
     */
    public function setCategoryAttr($category): ?string
    {
        if (is_string($category) || is_null($category) || !is_array($category)) return $category;

        $cates = [];
        foreach ($category as $v) {
            if (is_array($v)) {
                array_push($cates, ...$v);
            } else {
                array_push($cates, $v);
            }
        }

        return count($cates) ? ',' . implode(',', array_unique($cates)) . ',' : '';
    }

    /**
     * 当前用户查询范围
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-07-03
     */
    public function scopeMid($query, $user_id = 0)
    {
        $user_id = $user_id ?: request()->mid;
        $query->where('user_id', $user_id);
    }
}
